package com.jeecg.modules.jmreport.test;

import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
@Slf4j
public class IpUtils {
    /**
     * 获取客户端真实IP地址
     * 处理代理、负载均衡等情况
     */
    public static String getRealClientIp(HttpServletRequest request) {
        if (request == null) {
            return getLocalIp();
        }

        // 从HTTP头中获取可能的IP地址
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
            log.info("Proxy-Client-IP : " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
            log.info("WL-Proxy-Client-IP : " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
            log.info("HTTP_CLIENT_IP : " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            log.info("HTTP_X_FORWARDED_FOR : " + ip);
        }

        // 如果没有通过代理，直接获取REMOTE_ADDR
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            log.info("getRemoteAddr :" + ip);
            // 处理127.0.0.1和0:0:0:0:0:0:0:1（IPv6的localhost）
            if ("127.0.0.1".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip)) {
                ip = getLocalIp();
            }
        }

//        OkHttpClient client = new OkHttpClient();
//        Request request1 = new Request.Builder()
//                .url("https://ifconfig.me/ip")
//                .build();
//        try (Response response = client.newCall(request1).execute()) {
//            String ip1 = response.body().string();
////            System.out.println("Your IP Address: " + ip);
//            log.info("OkHttpClient request1 :" + ip1);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        Request request2 = new Request.Builder()
//                .url("https://ipinfo.io/json")
//                .build();
//        try (Response response = client.newCall(request2).execute()) {
//            String ip2 = response.body().string();
////            System.out.println("Your IP Address: " + ip);
//            log.info("OkHttpClient request2 :" + ip2);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        Request request3 = new Request.Builder()
//                .url("https://ipecho.net/plain")
//                .build();
//        try (Response response = client.newCall(request3).execute()) {
//            String ip3 = response.body().string();
////            System.out.println("Your IP Address: " + ip);
//            log.info("OkHttpClient request3 :" + ip3);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        // 多级代理情况下，第一个IP为客户端真实IP
        if (ip != null && ip.contains(",")) {
            ip = ip.split(",")[0].trim();
        }

        return ip;
    }

    /**
     * 获取本地机器的IP地址
     */
    private static String getLocalIp() {
        try {
            InetAddress inet = InetAddress.getLocalHost();
            return inet.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return "127.0.0.1";
        }
    }
}